package com.tjtt.tdm.system.dao.impl;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.HibernateEntityDAO;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.system.bean.Role;
import com.tjtt.tdm.system.bean.UserRole;
import com.tjtt.tdm.system.dao.RoleDao;

public class RoleDaoImpl extends HibernateEntityDAO<Role> implements RoleDao {

	//通过主键id查询角色信息
	public Role getRoleById(long roleid) {
		 Role role=(Role) super.getHibernateTemplate().get(Role.class ,roleid);
		 return role ;
	}

	@Override
	public List<Role> findRoleTree(Map filterMap) throws DaoException {
		String hql = "from Role as entity where 1=1 and entity.roleid<>4 ";
		if(filterMap!=null){
			if(filterMap.get("roleId")!=null); 
			hql +=" and entity.roleid = " + filterMap.get("roleId");
		}
		hql += " order by entity.roleid";		
		return this.findByHql(hql);
	}

	//查询角色名称是否存在
	/*public PageResult findRoleByName(String rolename,Map<String, String> filterMap) throws DaoException {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryString.append(" select r.roleid,r.rolename from sys_role r where r.rolename ='"+rolename+"'");
		queryCountString.append(" select count(*) from sys_role r where r.rolename ='"+rolename+"'");
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}*/

	@Override
	public Role findRoleByName(String rName) {
		List roleList = this.getHibernateTemplate().find("from Role r where r.rolename='" + StringUtil.escapeString(rName) + "'");
		if (roleList != null && roleList.size() > 0) {
			return (Role) roleList.get(0);
		}
		return null;
	}

	//查询角色下用户
	public PageResult searchUserByRole(Long roleid, Map<String, String> filterMap){
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryString.append("select t.userid, t.username,o.org_id,o.org_name from sys_users t left join sys_organization o on t.org_id = o.org_id where t.userid in (select t1.userid from sys_user_role t1 where t1.roleid = "+roleid+")");
		queryCountString.append(" select count(*) from (select t.userid, t.username,o.org_id,o.org_name from sys_users t left join sys_organization o on t.org_id = o.org_id where t.userid in (select t1.userid from sys_user_role t1 where t1.roleid = "+roleid+")) ");		
		if (filterMap != null && !filterMap.isEmpty()) {			
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	
	public PageResult searchUserList(Map<String, String> filterMap) {
		
		return null;
	}
	public UserRole saveUserToRole(UserRole userRole) {		
		return (UserRole) this.getHibernateTemplate().save(userRole);
	}

	@Override
	public void delete(UserRole userRole) {
		this.getHibernateTemplate().delete(userRole);
	}

	public PageResult queryUserList(Long roleid, Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
			queryString.append("select t.userid, t.username,o.org_id,o.org_name from sys_users t left join sys_organization o on t.org_id = o.org_id where t.del_flag=1 and t.userid not in (select t1.userid from sys_user_role t1 where t1.roleid = "+roleid+")  ");
			queryCountString.append(" select count(*) from (select o.org_id, t.username,t.userid,o.org_name from sys_users t left join sys_organization o on t.org_id = o.org_id where t.del_flag=1 and t.userid not in (select t1.userid from sys_user_role t1 where t1.roleid = "+roleid+") ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("username") &&! "".equals(filterMap.get("username"))){
				queryString.append("and t.username like '%" + filterMap.get("username") + "%'  ");
				queryCountString.append("and t.username like '%" + filterMap.get("username") + "%'  ");
			}if(null != filterMap.get("orgId") &&! "".equals(filterMap.get("orgId"))){
				queryString.append("and o.org_id = '" + filterMap.get("orgId") + "'  ");
				queryCountString.append("and o.org_id = '" + filterMap.get("orgId") + "'  ");
			}if(null != filterMap.get("toIds") &&! "".equals(filterMap.get("toIds"))){
				queryString.append("and t.userid not in (" + filterMap.get("toIds") + ")");
				queryCountString.append("and t.userid not in (" + filterMap.get("toIds") + ")");
		    }
		}
		queryString.append("order by o.org_id desc  ");
		queryCountString.append(")");
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	public List queryListBySql(String sql) {
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}
	
	//根据角色查询用户
	public PageResult queryUserByRole(String roleid, Map<String, String> filterMap){
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryString.append("select distinct t.userid, t.username,t.LOGINNAME as userloginname,o.org_id,o.org_name from sys_users t left join sys_organization o on t.org_id = o.org_id where 1=1 ");
		queryCountString.append(" select count(*) from (select distinct t.userid, t.username,t.LOGINNAME as userloginname,o.org_id,o.org_name from sys_users t left join sys_organization o on t.org_id = o.org_id where 1=1 ");		
		if(null!=roleid&&!"".equals(roleid)&&!"null".equals(roleid)){
			queryString.append(" and t.userid in (select t1.userid from sys_user_role t1 where t1.roleid = "+roleid+")");
			queryCountString.append(" and t.userid in (select t1.userid from sys_user_role t1 where t1.roleid = "+roleid+")");		
		}
		
		if (filterMap != null && !filterMap.isEmpty()) {	
			
			if(null != filterMap.get("userName") &&! "".equals(filterMap.get("userName"))){
				queryString.append("and t.username like '%" + filterMap.get("userName") + "%'  ");
				queryCountString.append("and t.username like '%" + filterMap.get("userName") + "%'  ");
			}
			if(null != filterMap.get("userLoginName") &&! "".equals(filterMap.get("userLoginName"))){
				queryString.append("and t.LOGINNAME like '%" + filterMap.get("userLoginName") + "%'  ");
				queryCountString.append("and t.LOGINNAME like '%" + filterMap.get("userLoginName") + "%'  ");
			}
			if(null != filterMap.get("orgId") &&! "".equals(filterMap.get("orgId"))){
				queryString.append(" start with o.org_id = '").append(filterMap.get("orgId")).append("' connect by prior o.org_id = o.org_parent_id ");
				queryCountString.append(" start with o.org_id = '").append(filterMap.get("orgId")).append("' connect by prior o.org_id = o.org_parent_id ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		queryCountString.append(")");
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

}
